type OnFufilled<V> = (value: V) => V | Promise<V>;
type OnRejected = (error: any) => any;

export interface Interceptor<V> {
  onFulfilled?: OnFufilled<V>;
  onRejected?: OnRejected;
}

// 这个地方是功能，同时可以当成类型来使用
class AxiosInterceptorManager<V> {
  // 存use的回调函数
  public interceptors: Array<Interceptor<V> | null> = [];

  use(onFulfilled?: OnFufilled<V>, onRejected?: OnRejected): number {
    // 存储回调函数
    this.interceptors.push({
      onFulfilled,
      onRejected,
    });
    // 同时返回{onFulfilled,onRejected,}的索引，会在eject使用
    return this.interceptors.length - 1;
  }
  eject(id: number) {
    if (this.interceptors[id]) {
      //   public interceptors: Array<Interceptor<V> | null> = [];有null的原因
      this.interceptors[id] = null;
    }
  }
}

export default AxiosInterceptorManager;
